<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <meta name="google-site-verification" content="k9iQUbEI9rWq3xYeh63ATztKdkthC4dNRHV_25maJ3Q" />
  <title>MCQ学习笔记 | Taylor&#39;s Learning Diary</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="初次接触MCQ源于蓝v，之前也听同事说微博的消息推送也是使用MCQ。认识MCQ之前感觉它很神秘，用之后才发现并也不过如此，所以很多知识不能只凭想象或者言传，需要自身实践才可知晓。这里想记录一下自己使用MCQ的过程，以及MCQ的使用场景、优缺点等等。  MemcacheQ - Simple Queue Service over Memcache在memcache基础上的简单队列服务，所以它兼具mem">
<meta name="keywords" content="memcacheq">
<meta property="og:type" content="article">
<meta property="og:title" content="MCQ学习笔记">
<meta property="og:url" content="https://upeng.github.io/blog/2016/07/11/memcacheq/index.html">
<meta property="og:site_name" content="Taylor&#39;s Learning Diary">
<meta property="og:description" content="初次接触MCQ源于蓝v，之前也听同事说微博的消息推送也是使用MCQ。认识MCQ之前感觉它很神秘，用之后才发现并也不过如此，所以很多知识不能只凭想象或者言传，需要自身实践才可知晓。这里想记录一下自己使用MCQ的过程，以及MCQ的使用场景、优缺点等等。  MemcacheQ - Simple Queue Service over Memcache在memcache基础上的简单队列服务，所以它兼具mem">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-06-28T16:12:44.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="MCQ学习笔记">
<meta name="twitter:description" content="初次接触MCQ源于蓝v，之前也听同事说微博的消息推送也是使用MCQ。认识MCQ之前感觉它很神秘，用之后才发现并也不过如此，所以很多知识不能只凭想象或者言传，需要自身实践才可知晓。这里想记录一下自己使用MCQ的过程，以及MCQ的使用场景、优缺点等等。  MemcacheQ - Simple Queue Service over Memcache在memcache基础上的简单队列服务，所以它兼具mem">
  
    <link rel="alternate" href="/atom.xml" title="Taylor&#39;s Learning Diary" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  

</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog/" id="logo">Taylor&#39;s Learning Diary</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog/">Home</a>
        
          <a class="main-nav-link" href="/blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://upeng.github.io/blog"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-memcacheq" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/blog/2016/07/11/memcacheq/" class="article-date">
  <time datetime="2016-07-11T15:29:55.000Z" itemprop="datePublished">2016-07-11</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/blog/categories/其他/">其他</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      MCQ学习笔记
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>初次接触MCQ源于蓝v，之前也听同事说微博的消息推送也是使用MCQ。认识MCQ之前感觉它很神秘，用之后才发现并也不过如此，所以很多知识不能只凭想象或者言传，需要自身实践才可知晓。这里想记录一下自己使用MCQ的过程，以及MCQ的使用场景、优缺点等等。</p>
<blockquote>
<p>MemcacheQ - Simple Queue Service over Memcache<br>在memcache基础上的简单队列服务，所以它兼具memcache和队列的特点。</p>
</blockquote>
<a id="more"></a>
<h2 id="Features-特性"><a href="#Features-特性" class="headerlink" title="Features[特性]"></a>Features[特性]</h2><ul>
<li>damn simple（炒鸡简单)</li>
<li>very fast（非常快）-&gt; 给予memcache,在内存中操作</li>
<li>multiple queue（多队列)</li>
<li>concurrent well（并发good)</li>
<li>memcache protocol compatible（兼容memcache协议)</li>
</ul>
<p>memcacheq(MCQ)是轻量级的消息队列，依赖于Berkeley DB和libevent。Berkeley DB用于持久化存储队列数据，避免MCQ宕机数据丢失。</p>
<h2 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景"></a>使用场景</h2><blockquote>
<p>高并发，数据可以弱一致性（弱一致性？）。</p>
</blockquote>
<p>蓝v后台沿用之前橙v逻辑（数据量大，并发高），也使用了MCQ。在高并发，大数据量的情况下，请求过多会发生阻塞，试想，在这种情况下，如果大量请求同时对mysql进行增删改，则会产生很大行锁（InnoDB）或表锁（Myisam）;请求堆积过多还可能会出现too many connections的错误。使用了MCQ，可以异步处理，怎么异步法？请求在不断的往队列塞（入队列），队列也不断地去消费这些请求（出队列），当然消费能力也是在mysql能力范围之内的，为啥？【思考缓冲作用】二者就处于异步状态。</p>
<p>支持多队列，比如我们创建了队列A，B，C，他们可以同时工作，其实这也体现了并发异步的特性。</p>
<h2 id="使用方法"><a href="#使用方法" class="headerlink" title="使用方法"></a>使用方法</h2><p>因为MCQ是基于memcached开发，所以可以使用memcached的命令对它进行各种操作；客户端比如php可以像操作memcacheq那样操作MCQ</p>
<h3 id="启动MCQ"><a href="#启动MCQ" class="headerlink" title="启动MCQ"></a>启动MCQ</h3><p><code>/usr/local/memcacheq/bin/memcacheq -d -r 1024 -l 127.0.0.1 -p 22201 -u root</code></p>
<p><strong>参数说明</strong><br><code>-d</code> 以守护进程方式运行<br><code>-r</code> 最大化核心文件限制<br><code>-l</code> 监听网卡<br><code>-p</code> TCP监听端口<br><code>-u</code> 以（root）身份运行</p>
<h3 id="最常用的mcq命令：set和get"><a href="#最常用的mcq命令：set和get" class="headerlink" title="最常用的mcq命令：set和get"></a>最常用的mcq命令：set和get</h3><figure class="highlight lsl"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">telnet <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span> <span class="number">22201</span> ....</div><div class="line"></div><div class="line">set testqueue <span class="number">0</span> <span class="number">0</span> <span class="number">3</span>    <span class="comment">//队列添加一条消息</span></div><div class="line">xxx</div><div class="line">STORED</div><div class="line"></div><div class="line">set testqueue <span class="number">0</span> <span class="number">0</span> <span class="number">5</span></div><div class="line">xxxxx</div><div class="line">STORED</div><div class="line"></div><div class="line">stats queue</div><div class="line">STAT testqueue <span class="number">2</span>/<span class="number">0</span></div><div class="line"></div><div class="line">get testqueue</div><div class="line">VALUE testqueue <span class="number">0</span> <span class="number">3</span></div><div class="line">xxx</div><div class="line">END</div><div class="line"></div><div class="line">stats queue</div><div class="line">STAT testqueue <span class="number">2</span>/<span class="number">1</span></div></pre></td></tr></table></figure>
<p><strong>注意</strong>：mcq没有expire time这个标记，是因为mcq支持数据持久化（Berkeley DB）。另外mcq存储长度length有限制（蓝v踩到的坑），之前同步前台用户认证申请材料至后台，因为材料增多，导致参数长度增加，超过了mcq的消息长度的限制：limit ? 后面分析查明</p>
<blockquote>
<p>MCQ和Redis的比较</p>
</blockquote>
<p><strong>memcacheq</strong></p>
<ul>
<li>数据保存在内存中，速度快</li>
<li>支持数据持久化</li>
<li>队列长度限制小</li>
<li>与memcache兼容</li>
<li>与memcached是两个独立服务，如同时使用需要分别安装</li>
</ul>
<p><strong>Redis</strong></p>
<ul>
<li>数据保存在内存中，速度快</li>
<li>支持数据持久化</li>
<li>队列长度限制小</li>
<li>支持多种数据类型</li>
<li>同时可以做为缓存来使用</li>
</ul>
<p>自己思考一下，在项目中MCQ和Redis各自的使用场景</p>
<p>后续阅读文章<br><a href="https://www.ibm.com/developerworks/cn/linux/l-ipc/" target="_blank" rel="external">深刻理解Linux进程间通信（IPC）</a><br><a href="http://blog.buaa.us/talk-about-mq/" target="_blank" rel="external">可靠消息队列浅谈</a><br><a href="http://www.fireflies.me/tag/%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97/" target="_blank" rel="external">MemcacheQ分析</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://upeng.github.io/blog/2016/07/11/memcacheq/" data-id="cj8x7817q001fgwp4c8re8qwt" class="article-share-link">Share</a>
      
        <a href="https://upeng.github.io/blog/2016/07/11/memcacheq/#disqus_thread" class="article-comment-link">留言</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/memcacheq/">memcacheq</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2016/07/13/vagrant/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          vagrant-快速搭建linux开发环境
        
      </div>
    </a>
  
  
    <a href="/blog/2016/06/30/zephir/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">使用zephir快速编写php扩展</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <div id="disqus_thread">
    <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  </div>
</section>
</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Linux/">Linux</a><span class="category-list-count">8</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/MySQL/">MySQL</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/PHP/">PHP</a><span class="category-list-count">15</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Thinking/">Thinking</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/其他/">其他</a><span class="category-list-count">8</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/前端/">前端</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/数据库范式/">数据库范式</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/blog/tags/CI/" style="font-size: 10px;">CI</a> <a href="/blog/tags/JQuery/" style="font-size: 10px;">JQuery</a> <a href="/blog/tags/Mac/" style="font-size: 13.33px;">Mac</a> <a href="/blog/tags/MySQL/" style="font-size: 10px;">MySQL</a> <a href="/blog/tags/awk/" style="font-size: 10px;">awk</a> <a href="/blog/tags/bash/" style="font-size: 10px;">bash</a> <a href="/blog/tags/composer/" style="font-size: 10px;">composer</a> <a href="/blog/tags/eloquent/" style="font-size: 10px;">eloquent</a> <a href="/blog/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/blog/tags/idempotence/" style="font-size: 10px;">idempotence</a> <a href="/blog/tags/item/" style="font-size: 10px;">item</a> <a href="/blog/tags/laravel/" style="font-size: 20px;">laravel</a> <a href="/blog/tags/linux/" style="font-size: 16.67px;">linux</a> <a href="/blog/tags/mac/" style="font-size: 10px;">mac</a> <a href="/blog/tags/memcacheq/" style="font-size: 10px;">memcacheq</a> <a href="/blog/tags/mysql/" style="font-size: 16.67px;">mysql</a> <a href="/blog/tags/nc/" style="font-size: 10px;">nc</a> <a href="/blog/tags/packageist/" style="font-size: 10px;">packageist</a> <a href="/blog/tags/php/" style="font-size: 13.33px;">php</a> <a href="/blog/tags/sed/" style="font-size: 10px;">sed</a> <a href="/blog/tags/shell/" style="font-size: 10px;">shell</a> <a href="/blog/tags/static/" style="font-size: 10px;">static</a> <a href="/blog/tags/thinking/" style="font-size: 13.33px;">thinking</a> <a href="/blog/tags/tmux/" style="font-size: 10px;">tmux</a> <a href="/blog/tags/vagrant/" style="font-size: 10px;">vagrant</a> <a href="/blog/tags/validator/" style="font-size: 10px;">validator</a> <a href="/blog/tags/vim/" style="font-size: 10px;">vim</a> <a href="/blog/tags/vuejs/" style="font-size: 10px;">vuejs</a> <a href="/blog/tags/zephir/" style="font-size: 10px;">zephir</a> <a href="/blog/tags/zsh/" style="font-size: 10px;">zsh</a> <a href="/blog/tags/设计模式/" style="font-size: 13.33px;">设计模式</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2017/10/">十月 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2017/09/">九月 2017</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/12/">十二月 2016</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/11/">十一月 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/10/">十月 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/09/">九月 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/08/">八月 2016</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/07/">七月 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/06/">六月 2016</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/05/">五月 2016</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/04/">四月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/02/">二月 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2016/01/">一月 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2015/10/">十月 2015</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2015/08/">八月 2015</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2015/07/">七月 2015</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog/2017/10/18/laravel-validator/">Laravel Validator自定义参数验证规则</a>
          </li>
        
          <li>
            <a href="/blog/2017/09/28/linux-sed/">linux常用命令之sed</a>
          </li>
        
          <li>
            <a href="/blog/2017/09/27/shell-script-learning/">shell脚本由点到面学习总结</a>
          </li>
        
          <li>
            <a href="/blog/2017/09/14/laravel-eloquent-index/">Eloquent ORM多个and和or条件查询</a>
          </li>
        
          <li>
            <a href="/blog/2017/09/10/Linux压缩解压缩命令-index/">Linux常用压缩解压缩命令</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2017 Tayloryu<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/blog/" class="mobile-nav-link">Home</a>
  
    <a href="/blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    
<script>
  var disqus_shortname = 'tayloryu';
  
  var disqus_url = 'https://upeng.github.io/blog/2016/07/11/memcacheq/';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">
  <script src="/blog/fancybox/jquery.fancybox.pack.js"></script>


<script src="/blog/js/script.js"></script>
  </div>
</body>
</html>